using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace FallenValkyrie {
    class GUI {
        //List<Texture2D> texture = new List<Texture2D>();
        //List<Vector2> position  = new List<Vector2>();
        //List<Rectangle> rect    = new List<Rectangle>();
        private static int max = 20;

        List<Color> skill_icon = new List<Color>();

        private int[] skill_cd = new int[max];
        private int[] skill_cost = new int[max];
        private SpriteFont font;
        private Texture2D[] texture = new Texture2D[max];
        private Vector2[] position = new Vector2[max];
        private Rectangle[] rect = new Rectangle[max];
        private bool[] skillUsed = new bool[max];
        private string[] name = new String[max];
        private int oriSkillCDLength;
        private int num_gui = 0;
        private int num_skill = 1;
        private float cd_elapsed = 0;
        private float mpRegenTimer;
        private Color skill_color = Color.White;

        public float mp_regenRate { get; set; }
        public int HP { get; set; }
        public int MP { get; set; }
        public int defMP { get; set; }

        public GUI(SpriteFont font) {
            this.font = font;
        }

        public void addSkill(int cooldown, int cost) {
            if (num_skill < max) {
                skill_cd[num_skill] = cooldown;
                skill_cost[num_skill] = cost;
                num_skill++;
            }
        }

        public void addItem(Texture2D item, Vector2 pos, string name, int width, int height) {
            if (num_gui < max) {
                texture[num_gui] = item;
                this.name[num_gui] = name;
                position[num_gui] = pos;

                rectCreator(position[num_gui], width, height, num_gui);
                num_gui++;
            }
        }

        public void updateGUI(int currentSkill, GameTime gt) {
            skillCooldown(currentSkill, gt);
            mpRegenerate(gt);
        }

        public void drawGUI(SpriteBatch sb, int hp, int mp) {
            sb.Begin();
            for (int i = 0; i < num_gui; i++) {
                if (name[i] == "HP Bar")
                    sb.Draw(texture[i], new Rectangle(rect[i].X, rect[i].Y, HP, rect[i].Height), Color.Red);

                else if (name[i] == "MP Bar")
                    sb.Draw(texture[i], new Rectangle(rect[i].X, rect[i].Y, MP, rect[i].Height), Color.Blue);

                else if (name[i] == "Skill") {
                    if (skillUsed[i])
                        sb.DrawString(font, skill_cd[i].ToString(), new Vector2(rect[i].Width / 2, rect[i].Height / 2), Color.White);

                    sb.Draw(texture[i], rect[i], skill_color);
                }
                else sb.Draw(texture[i], rect[i], Color.White);
            }
            sb.End();
        }


        private void skillCooldown(int currentSkill, GameTime gt) {

            if (MP > skill_cost[currentSkill]) {
                if (!skillUsed[currentSkill]) {
                    skillUsed[currentSkill] = true;
                    MP -= skill_cost[currentSkill];
                }
                if (skillUsed[currentSkill]) {
                    skill_color = Color.Black;
                    oriSkillCDLength = skill_cd[currentSkill];
                    if (skill_cd[currentSkill] > 0)
                        skill_cd[currentSkill] -= gt.ElapsedGameTime.Milliseconds;
                    else if (skill_cd[currentSkill] <= 0) {
                        skillUsed[currentSkill] = false;
                        skill_cd[currentSkill] = oriSkillCDLength;
                    }
                }
            }
        }


        private void mpRegenerate(GameTime gt) {
            if (MP < defMP) {
                if (mpRegenTimer < mp_regenRate)
                    mpRegenTimer += gt.ElapsedGameTime.Milliseconds;
                else if (mpRegenTimer >= mp_regenRate) {
                    if (MP < defMP) MP++;
                    if (MP == defMP) mpRegenTimer = 0;
                }
            }
        }

        private void rectCreator(Vector2 pos, int width, int height, int num) {
            rect[num] = new Rectangle((int)pos.X, (int)pos.Y, width, height);
        }
    }
}
